B1: Effects and Handlers with Koka or Flix
Writing programs in a purely functional language comes with some advantages especially when we want to reason about the program. This is manly thanks to the fact that in purely functional languages functions are pure and therefore some function invocation `f x` always produces the same result because it is not able to alter any internal state.
Many programs, however, cannot be modelleded 100% as a completely determinstic mathematically pure function. For example, any program requiring IO (read and write to files, database, network communication), taking external actions such as (performing robotic actions, taking a camera picture), or getting truly random numbers. Other things that go beyond what pure functions can do, are nonterminating functions (the are the ability to have an infinite loop), or nondeterministic and probabilistic evaluation (to perform a coin flip, and return both results at the end of the function, exploring all combinatorial possibilities).
These things are called (side)-effects.
There have been different ways to handle effects
- Classical languages, such as Python, C, and C++, Rust, Java, Scala, JavaScript were designed with a pre-built set of side-effects (mutable state, IO, randomness, etc) with builtin keywords to activate them, and it is very hard or impossible to add further effects later on (async/await), leading to strange and undefined interactions between them
- In some programming languages, there are libraries that allow to model user-defined effects, (e.g., Monads in Haskell, Monads in Scala, Monad Transformer Libraries, Freer Monads)
- Finally, and this is the point of this topics, some programming languages are speficically designed to allow the user to define their own effects and use them (e.g., Koka, Flix)
In this Seminar topic you will learn one of the two languages Koka or Flix and research on the advantages and disadvantages of those effect languages.